package com.lan.iot;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static com.lan.iot.Activity_index.PREFS_NAME;

public class Login extends AppCompatActivity {
    boolean isHideFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        final ImageView btc = findViewById(R.id.display_password);
        btc.setImageResource(R.drawable.close);
        final EditText passw = findViewById(R.id.ed_psw);
        TransformationMethod method = PasswordTransformationMethod.getInstance();
        passw.setTransformationMethod(method);
        btc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isHideFirst) {
                    btc.setImageResource(R.drawable.open);
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    passw.setTransformationMethod(method1);
                    isHideFirst = false;
                } else {
                    btc.setImageResource(R.drawable.close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    passw.setTransformationMethod(method);
                    isHideFirst = true;
                }
                int index = passw.getText().toString().length();
                passw.setSelection(index);
            }
        });
        TextView nh = findViewById(R.id.text_nh);
        nh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(Login.this, Signin.class);
                startActivity(intent);
                finish();
            }
        });
        final EditText edtel = findViewById(R.id.ed_tel);
        final EditText edpsw = findViewById(R.id.ed_psw);
        Button btn = findViewById(R.id.login);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tel = String.valueOf(edtel.getText());
                String psw = String.valueOf(edpsw.getText());
                final SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
                String tel_share = settings.getString("tel", "");
                String psw_share = settings.getString("psw", "");
                if (tel.length() < 2 || psw.length() < 2 || (!tel.equals(tel_share)) || (!psw.equals(psw_share))) {
                            Toast.makeText(Login.this,"手机号或密码错误", Toast.LENGTH_SHORT).show();

                } else {
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("f", "f");
                    editor.commit();
                    Intent intent = new Intent();
                    intent.setClass(Login.this, Activity_index.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
}
